# coding:utf-8

import os
import urllib

from alipay import AliPay

APP_ID = os.environ.get('alipay_app_id') or '2017030306033115'

SIGN_TYPE = 'RSA2'

ALI_API_SERVER = 'https://openapi.alipay.com/gateway.do'

ALI_PRIVATE_KEY_PATH = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), 'alipay_private_key'
)

ALI_PUBLIC_KEY_PATH = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), 'alipay_public_key'
)

ALI_NOTIFY_URL = urllib.parse.urljoin(
    os.environ.get('opc_host') or 'http://localhost', 'api/Supplier/AliPay/notify'
)

alipay_client = AliPay(
    appid=APP_ID,
    app_notify_url=ALI_NOTIFY_URL,
    app_private_key_path=ALI_PRIVATE_KEY_PATH,
    alipay_public_key_path=ALI_PUBLIC_KEY_PATH,  # 支付宝的公钥，验证支付宝回传消息使用，不是你自己的公钥,
    sign_type=SIGN_TYPE,  # RSA 或者 RSA2
    debug=False  # 默认False
)


def create_pay_url(out_trade_no, amount, subject, body, return_url):
    """
    created by chenhj at 2017/8/20

    """
    order_string = alipay_client.api_alipay_trade_wap_pay(
        out_trade_no=out_trade_no,
        total_amount=amount,
        subject=subject,
        return_url=return_url,
        body=body
    )
    return ALI_API_SERVER + '?' + order_string
